package com.ezlearning.platform.controller;
import com.ezlearning.platform.dto.StudentDto;
import com.ezlearning.platform.model.Student;
import com.ezlearning.platform.repositories.CursoRepository;
import com.ezlearning.platform.repositories.StudentRepository;
import com.ezlearning.platform.services.core.impl.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/student")
@Slf4j
public class StudentController {
    private StudentService studentService;
    private StudentRepository studentRepository;
    private CursoRepository cursoRepository;

    @Autowired
    public StudentController(StudentService studentService, StudentRepository studentRepository,
                             CursoRepository cursoRepository) {
        //Profesor profesor = new Profesor();
        this.studentService = studentService;
        this.studentRepository = studentRepository;
        this.cursoRepository = cursoRepository;
    }

    @GetMapping("/add")
    public String addStudent(Model model) {
        model.addAttribute("student", new StudentDto());
        return "student/student-add";
    }

    @PostMapping("/save")
    public String saveStudent(StudentDto student) {
        log.info("新增学生:{}",student);
        studentService.create(student);

        return "redirect:/student";
    }

    @GetMapping("/edit/{id_student}")
    public String getStudentForUpdate(@PathVariable Long id_student,
                                       Model model) {
        try {
            Student student = studentRepository.findById(id_student).get();
            model.addAttribute("student", student);
            return "student/student-edit";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @PostMapping("/update/{id_student}")
    public String updateStudent(@PathVariable Long id_student,
                                 Student student, RedirectAttributes attributes, Model model){
        student.setStudentId(id_student);
        try {
            Student currentstudent = studentRepository.findById(id_student).get();
            currentstudent.setNombre(student.getNombre());
            currentstudent.setApellido(student.getApellido());
            currentstudent.setCorreo(student.getCorreo());
            currentstudent.setDescripcion(student.getDescripcion());
            currentstudent.setImgurl(student.getImgurl());

            studentService.update(student);
            attributes.addAttribute("id_student", id_student);

            return "redirect:/student/{id_student}";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @PostMapping("/patch/{id_student}")
    public String patchStudent(@PathVariable Long id_student, Student student, RedirectAttributes attributes, Model model) {

        try {
            Student current = studentRepository.findById(id_student).get();
            current.setDetalle(student.getDetalle());
            studentService.patch(current);

            attributes.addAttribute("id_student", id_student);
            return "redirect:/student/{id_student}";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }

    @GetMapping
    public String getStudentList(Model model) {
        List<Student> students = studentService.getAll();
        model.addAttribute("students", students);
        return "student/student";
    }

    @GetMapping("/delete/{id_student}")
    public String deleteStudent(@PathVariable Long id_student, Model model) {
        log.info("删除学生id:{}",id_student);
        try {
            Student student = studentRepository.findById(id_student).get();
            studentService.delete(student);

            return "redirect:/student";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error",e);
            return "error";
        }
    }

    @GetMapping("/{id_student}")
    public String getStudentDetail(@PathVariable Long id_student, Model model) {
        try {
            Student student = studentRepository.findById(id_student).get();
            model.addAttribute("student", student);
            /*List<Student> cursos = cursoRepository.findAllByStudent(student);
            model.addAttribute("cursos", cursos);*/
            return "student/student-detail";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", e);
            return "error";
        }
    }
}
